<template>
  <div class="app-container customer-div">
    <el-form :inline="true"  :model="listQuery" size="mini" label-width="100px" style="margin-top:20px">
          <el-form-item label="日期：" >
            <el-date-picker
              :clearable="false"
              :format="dataFormat"
              :value-format="dataFormat"
              v-model="listQuery.fmMonth"
              type="monthrange"
              align="center"
              unlink-panels
              range-separator="~"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              size="small"
              class="date-class"
              prefix-icon="calendar-icon">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="业务员：" >
            <el-select style="width:100px" v-model="listQuery.userId" filterable clearable>
              <el-option
                v-for="item in userOptionList"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>

        <el-form-item label="部门：" >
          <el-cascader
            v-model="listQuery.departmentId"
            :options="data"
            :props="{value:'departmentId',label:'departmentName',emitPath:false}"
            clearable>
          </el-cascader>
        </el-form-item>

          <el-form-item>
            <el-button
              style="margin-left:10px;"
              @click="handleSearchList"
              type="primary">
              查询
            </el-button>
            <el-button
              :loading="btnloading"
              @click="downloadExcel"
              type="primary">
              导出
            </el-button>
          </el-form-item>
        </el-form>


        <!--数据列表-->
        <div class="table-container">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="tableData"
            border
            style="width: 100%; margin-top: 20px">
            <el-table-column  label="月份"  align="center" min-width="100">
              <template slot-scope="scope">
                  {{scope.row.fmMonth}}
              </template>
            </el-table-column>
            <el-table-column align="center" label="业务员" min-width="100">
              <template slot-scope="scope">
                {{scope.row.userName}}
              </template>
            </el-table-column>
            <el-table-column label="部门" align="center" min-width="100">
              <template slot-scope="scope">
                {{scope.row.departmentName}}
              </template>
            </el-table-column>
            <el-table-column min-width="100"  align="center">
              <template slot="header" slot-scope="scope">
                <div>
                  合同总量 <el-tooltip class="item" effect="dark" content="当月合同：包含流转完成，作废" placement="right">
                  <i class="el-icon-question"></i>
                </el-tooltip>
                </div>
              </template>
              <template slot-scope="scope">
                {{scope.row.allOrder}}
              </template>
            </el-table-column>
            <el-table-column min-width="100" align="center">
              <template slot="header" slot-scope="scope">
                <div>
                  合同有效量 <el-tooltip class="item" effect="dark" content="流转完成合同数量" placement="right">
                  <i class="el-icon-question"></i>
                </el-tooltip>
                </div>
              </template>

              <template slot-scope="scope">
                {{scope.row.validOrder}}
              </template>
            </el-table-column>
            <el-table-column min-width="100"  align="center">
              <template slot="header" slot-scope="scope">
                <div>
                  合同取消总量 <el-tooltip class="item" effect="dark" content="合同取消时间为当月合同数量" placement="right">
                  <i class="el-icon-question"></i>
                </el-tooltip>
                </div>
              </template>
              <template slot-scope="scope">
                {{scope.row.allCancelOrder}}
              </template>
            </el-table-column>
            <el-table-column min-width="100" align="center">
              <template slot="header" slot-scope="scope">
                <div>
                  当月合同取消总量 <el-tooltip class="item" effect="dark" content="当月订单取消数量" placement="right">
                  <i class="el-icon-question"></i>
                </el-tooltip>
                </div>
              </template>
              <template slot-scope="scope">
                {{scope.row.cancelOrder}}
              </template>
            </el-table-column>
            <el-table-column min-width="100" align="center">
              <template slot="header" slot-scope="scope">
                <div>
                  财务审核数量 <el-tooltip class="item" effect="dark" content="当月审核订单数量包含其它月份当月审核" placement="right">
                  <i class="el-icon-question"></i>
                </el-tooltip>
                </div>
              </template>
              <template slot-scope="scope">
                {{scope.row.allFinanceOrder}}
              </template>
            </el-table-column>
            <el-table-column min-width="100" label="财务审核金额" align="center">
              <template slot-scope="scope">
                {{scope.row.financePrice}}
              </template>
            </el-table-column>
            <el-table-column min-width="100"  align="center">
              <template slot="header" slot-scope="scope">
                <div>
                  当月财务审核数量 <el-tooltip class="item" effect="dark" content="当月订单，当月审核数量" placement="right">
                  <i class="el-icon-question"></i>
                </el-tooltip>
                </div>
              </template>
              <template slot-scope="scope">
                {{scope.row.financeOrder}}
              </template>
            </el-table-column>
            <el-table-column min-width="100"  align="center">
              <template slot="header" slot-scope="scope">
                <div>
                  审核取消数 <el-tooltip class="item" effect="dark" content="财务状态为已审核 ，状态为作废，且取消时间为当月" placement="right">
                  <i class="el-icon-question"></i>
                </el-tooltip>
                </div>
              </template>
              <template slot-scope="scope">
                {{scope.row.financeCancelOrder}}
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination-container">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes,prev, pager, next,jumper"
            :current-page.sync="listQuery.pageNum"
            :page-size="listQuery.pageSize"
            :page-sizes="[10,15,20,50,100]"
            :total="count">
          </el-pagination>
        </div>

  </div>
</template>

<script>
const defaultListQuery={
  departmentId:'',
  "fmMonth" : [],  //时间
  userId:'',
  "pageNum"    : 1,
  "pageSize"   : 10
};

import {financechartList,financeMonth} from '@/api/company'
import {getOrderUsersList} from '@/api/partner'
import {numberToCurrencyNo} from '@/utils/index'
import {getDeptList} from '@/api/management'
import store from '@/store'
import { mapGetters } from 'vuex'

export default {
  name: "saleReport",
  data(){
    return{
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      tableData: [],
      count: 0,
      listLoading: false,
      roles:store.getters.roles ? store.getters.roles : [],
      userOptionList:[],
      dataFormat:'yyyy-MM',
      btnloading:false,
      data:[],
      dataLoading:false
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'mobile',
      'userId'
    ]),
  },
  created() {
    this.getList();
    this.getUserOptionList();
    this.getBuMenList()
  },
  filters: {
     fifnum(val){
       if(val){
         return numberToCurrencyNo(val)
       }else{
         return ''
       }
     }
  },
  methods:{
    //去除不要的children
    forTree(list){
      let newList = list;
      if(newList.length){
        for (let i = 0; i<newList.length; i++){
          if(newList[i].children && newList[i].children.length > 0){
            this.forTree(newList[i].children)
          }else{
            delete newList[i].children;
          }
        }
        return newList
      }
    },

    //获取部门列表
    getBuMenList(){
      getDeptList().then((res)=>{
        this.data = this.forTree(res);
      })
    },

    //获取员工下拉选项
    getUserOptionList(){
      getOrderUsersList().then((res)=>{
        this.userOptionList = res;
      })
    },

    //下载excle
    downloadExcel(){
      let params
      params = JSON.parse(JSON.stringify(this.listQuery));
      params.export = 1;
      delete params.pageSize;
      delete params.pageNum;
      financeMonth(params).then((res)=>{
        this.btnloading = false;
        require.ensure([], () => {
          const { export_json_to_excel } = require('sheet/Export2Excel');
          const tHeader = res.title;
          const filterVal = res.columns;
          const list = res.list;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, res.fileName);
        })
      }).catch(()=>{
        this.btnloading = false;
      })
    },

    //下载表格数据格式化
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },



    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
      this.getChartList()
    },

    //获取列表
    getList() {
      this.listLoading = true;
      financeMonth(this.listQuery).then(response => {
        this.tableData = response.list;
        this.count = response.count;
        this.listLoading = false;
      }).catch(()=>{
        this.listLoading = false;
      });
    },



  },
}
</script>

<style scoped lang="scss">

</style>
